﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION dbo.f_GetInvOutRest 
(
	-- Add the parameters for the function here
	@InvId int
)
RETURNS money
AS
BEGIN
	-- Declare the return variable here
	DECLARE @Rest money
	SET @Rest = 0
	-- Add the T-SQL statements to compute the return value here
	SELECT @Rest = (invO.[SUM] - ISNULL(SUM(pioi.SUM_CUR),0)) FROM INVOICE_OUT as invO 
			LEFT JOIN PAY_IN_ON_INV_OUT as pioi ON pioi.INVOICE_OUT_ID = invO.ID 
	WHERE invO.ID = @InvId
	GROUP BY invO.[SUM]
	-- Return the result of the function
	RETURN @Rest

END